/**
 * Created with JetBrains WebStorm. User: zhaolei Date: 13-3-29 Time: 上午9:29 To
 * change this template use File | Settings | File Templates.
 */
$(function () {
    //loading
    var url = window.location.host;
    var loading = '<div class="loading tc"><img width="20" src="http://' + url + '/WSZF/images/v4/loading.gif" alt=""></div>';

    function login() {
    	/**加密狗开始*/
    	//var returnFlag = OnLogon();
    	//if(!returnFlag) return;
       // var $uid = $("#uid").val();
        //var $ClientDigest = $("#ClientDigest").val();
        /**加密狗结束*/
        /*此处不能添加trim()方法，因为在IE8的情况下无法识别，所以说直接去掉即可*/
        //var $userId = $(".j-inputUsername").val().trim();
    	
    	var $userId = $(".j-inputUsername").val();
        var $password = $(".j-inputPsw").val();
        var $error = $('.j-loginError');
        var certList="";
        var certSN="";
       // $error.css({'backgroundColor':'#f5f5f5'}).html(loading).show();
        if(isUKey()){
        	certList = $("#CertList").val();
        	certSN=$("#CertSN").val();
        	$.ajax({
                type: "post",
                url: "json/AuthorityAction!findUKey",
                data:{"certSN": certSN},
                async: true,
                success: function (data) {
                    if (null != data.clazz) {
                        var datasource = data.clazz;
                        $userId=datasource.usId;
                        $password = '00000000'
                    }else {
                        $error.html('<em class="pl20 f12 ml4">'+data.msg+'</em>').show();
                    }
                }
            });
	    }
        
        if ($userId == null || $.trim($userId).length == 0) {
            $error.html('<em class="pl20 f12 ml4">用户名不能为空</em>').show();
            return;
        }
        if ($password == null || $.trim($password).length == 0) {
            $error.html('<em class="pl20 f12 ml4">密码不能为空</em>').show();
            return;
        }
        $.ajax({
            type: "post",
            url: "json/AuthorityAction!logon",
            data:{"userId": $userId,"password": $password,"sessionId": new Date().getTime()},
            async: true,
            success: function (data) {
                if ("success"==data.msg) {
                	window.location = 'page/main_page'
                }else {
                	$error.html('<em class="pl20 f12 ml4">用户名或密码错误，请重新输入。</em>').show();
                }
            }
        });
        /*$.post("json/AuthorityAction!logon", {//一般url都是要走web.xml的，在web.xml中找到/json/*的filter对应struts中的action
        	//但是struts中的action和对应的方法都被springMvc包装过了，即实例化的类都用代号来命名
            "userId": $userId,
            "password": $password,
            "sessionId": new Date().getTime()//,
            *//**加密狗开始*//*// "uid":$uid,
            //"ClientDigest":$ClientDigest/**加密狗结束
        }, function (msg) {//回调函数，参数是从后台结果返回值得到的，此处返回dataresult,作为参数传进来
            if (msg.success) {//dataresult中有success属性，为boolean型，如果为true则跳转到main.jsp
                window.location = 'page/main_page'
            } else {
                $error.html('<em class="pl20 f12 ml4">用户名或密码错误，请重新输入。</em>').show();//<em>标签的含义指的是将以斜体强调格式显示标签中的内容
            }
        });*/

    }

    document.onkeydown = function (e) {
        var ev = document.all ? window.event : e;
        if (ev.keyCode == 13) {
            login();
        }
    };
    var $error = $('.j-loginError');
    var $loginBtn = $('.j-loginBtn');//定义变量
    $('input').focus(function () {
        $(this).css('background-position', '0 -40px');
        $error.hide();
    }).blur(function () {
            $(this).css('background-position', '0 0')
        });
    $loginBtn.hover(function () {
        $(this).css('background-position', '0 -40px');
    }, function () {
        $(this).css('background-position', '0 0');
    });
    $loginBtn.mousedown(function () {
        $(this).css('background-position', '0 -80px');
    }).mouseup(function () {
            $(this).css('background-position', '0 -40px');
        });
    $loginBtn.click(function () {
        login();
    });
    $(window).load(function () {
        $('.j-inputUsername').focus().val('');
    })
});